﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ProjectWebNhac.DAO;
using System.Data;
using ProjectWebNhac.DTO;

namespace ProjectWebNhac.BUS
{
    public class BaiHatBus
    {
        Dataprovider dao = new Dataprovider();
        DataTable dt;
        CaSiBus csBus = new CaSiBus();
        
        public DataTable LoadDanhSachBaiHat()
        {
            dt = dao.LayDuLieuThamSo("sp_Admin_LayDanhSachBaiHat", CommandType.StoredProcedure);
            return dt;
        }

        public DataTable LoadDanhSachBaiHat_User()
        {
            dt = dao.LayDuLieuThamSo("sp_User_LayDanhSachBaiHat", CommandType.StoredProcedure);
            return dt;
        }

        public DataTable LoadDanhSachBaiHatTheoUser(string TaiKhoan)
        {
            object[] para = { "@TaiKhoan", TaiKhoan };
            dt = dao.LayDuLieuThamSo("sp_User_LayDanhSachBaiHatTheoUser", CommandType.StoredProcedure, para);
            return dt;
        }

        //public bool UpdateLuotNghe(int MaBaiHat)
        //{
        //    object[] para = { "@MaBaiHat", MaBaiHat };
        //    dt = dao.LayDuLieuThamSo("sp_User_LayLuotNgheBaiHat", CommandType.StoredProcedure, para);
        //    int luotnghe=int.Parse(dt.Rows[0][0].ToString())+1;
        //    object[] para1= {"@MaBaiHat",MaBaiHat,"@LuotNghe",luotnghe};
        //    bool kq = dao.XuLySuKien("sp_User_UpdateLuotNghe", CommandType.StoredProcedure, para1);
        //    return kq;
        //}

        public DataTable LoadDanhSachBaiHatMoiTop20()
        {
            dt = dao.LayDuLieuThamSo("sp_User_LayDanhSachBaiHatMoiTop20", CommandType.StoredProcedure);
            return dt;
        }

        public bool ThemBaiHat(BaiHatDTO objBaiHat)
        {
            bool isSuccess = false;
            isSuccess = dao.XuLySuKien("sp_Admin_ThemBaiHat", CommandType.StoredProcedure,
               new object[] {"@TenBaiHat",objBaiHat.TenBaiHat,"@Lyric",objBaiHat.Lyrics,
               "@TaiKhoan",objBaiHat.TaiKhoan,"@LinkBaiHat",objBaiHat.LinkBaiHat,
               "@MaTheLoai",objBaiHat.MaTheLoai ,"@TrangThai",objBaiHat.TrangThai});
            if (isSuccess)
            {
                int MaBH = Convert.ToInt16(dao.LayDuLieuThamSo("sp_Admin_LayMaBaiHatMoiNhat",
                    CommandType.StoredProcedure).Rows[0][0]);
                foreach (var maCasi in csBus.LayMaCasiTheoTen(objBaiHat.TenCaSi))
                {
                    isSuccess = dao.XuLySuKien("sp_Admin_ThemCaSiBaiHat", CommandType.StoredProcedure,
                        new object[] { "@MaCaSi", maCasi, "@MaBH",MaBH });
                }
            }
            return isSuccess;
        }

        public bool SuaBaiHat(BaiHatDTO objBaiHat)
        {
            bool isSuccess = false;
            isSuccess = dao.XuLySuKien("sp_Admin_SuaBaiHat", CommandType.StoredProcedure,
               new object[] {"@MaBaiHat",objBaiHat.MaBaiHat,"@TenBaiHat",objBaiHat.TenBaiHat,
               "@Lyrics",objBaiHat.Lyrics,"@MaTheLoai",objBaiHat.MaTheLoai });

                //if (isSuccess)
            {
                isSuccess = dao.XuLySuKien("sp_Admin_XoaCaSiBaiHatTheoMaBaiHat",
                    CommandType.StoredProcedure, new object[] { "@MaBaiHat", objBaiHat.MaBaiHat });
            }
            foreach (var maCasi in csBus.LayMaCasiTheoTen(objBaiHat.TenCaSi))
            {
                isSuccess = dao.XuLySuKien("sp_Admin_ThemCaSiBaiHat", CommandType.StoredProcedure,
                        new object[] { "@MaCaSi", maCasi, "@MaBH", objBaiHat.MaBaiHat });
            }
            return isSuccess;
        }

        public bool XoaBai(int MaBaiHat)
        {
            bool isSuccess = dao.XuLySuKien("sp_Admin_XoaBaiHatTheoMaBaiHat",
                    CommandType.StoredProcedure, new object[] { "@MaBaiHat", MaBaiHat });
            return isSuccess;
        }

        public bool XacNhanBaiHat(int MaBaiHat)
        {
            bool isSuccess = dao.XuLySuKien("sp_Admin_XacNhanBaiHatTheoMaBaiHat",
                    CommandType.StoredProcedure, new object[] { "@MaBaiHat", MaBaiHat });
            return isSuccess;
        }

        public DataTable LoadChiTietBaiHat(int MaBaiHat)
        {
            dt = dao.LayDuLieuThamSo("sp_Admin_LayChiTietBaiHat", CommandType.StoredProcedure,new object[] {"@MaBaiHat",MaBaiHat});
            return dt;
        }

        public DataTable LoadDanhSachBaiHatTheoAlbum(int MaAlbum)
        {
            dt = dao.LayDuLieuThamSo("sp_Admin_LayDanhSachBaiHatTheoAlbum", CommandType.StoredProcedure, new object[] { "@MaAlbum", MaAlbum });
            return dt;
        }

        public DataTable LoadDanhSachBaiHatTheoCaSi(int MaCaSi)
        {
            dt = dao.LayDuLieuThamSo("sp_User_LayDanhSachBaiHatTheoCaSi", CommandType.StoredProcedure, new object[] { "@MaCaSi", MaCaSi });
            return dt;
        }

        public DataTable LoadDanhSachBaiHatTheoTheLoai(int MaTheLoai)
        {
            dt = dao.LayDuLieuThamSo("sp_User_LayDanhSachBaiHatTheoTheLoai", CommandType.StoredProcedure, new object[] { "@MaTheLoai", MaTheLoai });
            return dt;
        }

        public DataTable User_LayChiTietBaiHat(int maBH)
        {
            return dao.LayDuLieuThamSo("sp_User_LayChiTietBaiHat", CommandType.StoredProcedure,
                new object[] { "@maBaiHat",maBH });
        }

        public DataTable User_LoadDsTop20()
        {
            return dao.LayDuLieuThamSo("sp_User_DsTop20BaiHat", CommandType.StoredProcedure);
        }

        public DataTable User_LoadBXHTop20()
        {
            return dao.LayDuLieuThamSo("sp_User_LayBXHTop20", CommandType.StoredProcedure);
        }

        public DataTable User_LoadBXHTop10()
        {
            return dao.LayDuLieuThamSo("sp_User_LayBXHTop10", CommandType.StoredProcedure);
        }

        public DataTable LayDanhSachBaiHatTheoAlbum(int MaAlbum)
        {
            return dao.LayDuLieuThamSo("sp_User_LayDSBaiHatTheoAlbum",
                CommandType.StoredProcedure, new object[] { "@MaAlbum", MaAlbum });
        }

        //Luu Du lieu phan
        public DataSet LayDanhSachBaiHatPT(int currPage,int recodperpage,int Pagesize,int MaTheLoai)
        {
            return dao.LayDsThamSo("sp_User_PhanTrangBaiHat", CommandType.StoredProcedure,
                new object[] { "@currPage", currPage, "@recodperpage", recodperpage, "Pagesize",
                    Pagesize,"@MaTheLoai",MaTheLoai });
        }

        public DataSet TimKiemTheoTen(int currPage, int recodperpage, int Pagesize, string keyword)
        {
            return dao.LayDsThamSo("sp_User_TimKiemTheoBaiHat", CommandType.StoredProcedure,
                new object[] { "@currPage", currPage, "@recodperpage", recodperpage, "Pagesize",
                    Pagesize,"@txtSearch",keyword });
        }

        public DataSet LayDanhSachBaiHatPTTheoCaSi(int currPage,int recodperpage,int Pagesize,int MaCaSi)
        {
            return dao.LayDsThamSo("sp_User_PhanTrangBaiHatTheoCaSi", CommandType.StoredProcedure,
               new object[] { "@currPage", currPage, "@recodperpage", recodperpage, "Pagesize",
                    Pagesize,"@MaCaSi",MaCaSi });
        }

        public DataTable LayDanhSachBHNoiBat()
        {
            return dao.LayDuLieuThamSo("sp_User_LayDanhSachBaiHatNoiBat",
                CommandType.StoredProcedure);
        }

        public DataTable LayDsBHThemAlbum()
        {
            return dao.LayDuLieuThamSo("sp_Admin_LayBaiHatThemAlbum",
                CommandType.StoredProcedure);
        }

    }
}